/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLogic;

import java.sql.SQLException;
import java.util.Vector;
import DTO.Booth;
import DTO.Exhibitor;
import DTO.Report;
import DTO.Zone;
import DataAccess.DataUtility;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.util.HashMap;

/**
 *
 * @author Tran anh
 * @version 1.0
 */
public class BoothManagement {

    /**
     * listBooth used to contain list booth get from database
     */
    private Vector<Booth> listBooth;

    /**
     * listExhibitor used to contain list exhibitor get from database
     */
    private Vector<Exhibitor> listExhibitor;

    /**
     * listZone used to contain list zone get from database
     */
    private Vector<Zone> listZone;

    /**
     * listReport used to contain list object report generate from informations
     */
    private Vector<Report> listReport;

    public BoothManagement() {
    }

    /**
     * Load list booths folow by idZone, then pass resultSet into
     * method getBooths to get listBooth.
     * @param idZone is identify zone to load booth
     * @param flag is load booth folow by idZone or nameExhibitor
     * @return list booth
     * @see #loadBooth(java.lang.String) 
     */
    public Vector<Booth> loadBooth(String idZone, boolean flag) {

        ResultSet rs = DataUtility.excuteQuery("Select * from Booth where status = 1 and idZone = " + "\'" + idZone + "\'");

        return getBooths(rs);
    }

    /**
     * Load list booths folow by nameExhibitor, then pass resultSet into
     * method getBooths to get listBooth.
     * @param nameExhibitor is identify Exhibitor to load booth
     * @return list booth
     * @see #loadBooth(java.lang.String, boolean) 
     */
    public Vector<Booth> loadBooth(String nameExhibitor) {
        String idExhibitor = getID(nameExhibitor);
        ResultSet rs = DataUtility.excuteQuery("Select * from Booth where status = 1 and idExhibitor = " + "\'" + idExhibitor + "\'");

        return getBooths(rs);
    }

    /**
     * Receive a resultSet and get listBooth from it
     * @param rs is resultSet where containt list booths
     * @return list booth
     */
    public Vector<Booth> getBooths(ResultSet rs) {
        try {
            listBooth = new Vector<Booth>();

            while (rs.next()) {
                Booth booth = new Booth();
                listBooth.add(booth);

                booth.setDateCreate(rs.getString("dateCreate"));
                booth.setId(rs.getString("id"));
                booth.setIdExhibitor(rs.getString("idExhibitor"));
                booth.setIdZone(rs.getString("idZone"));
                booth.setMoneyService(rs.getFloat("moneyService"));
                booth.setName(rs.getString("name"));
                booth.setNumberStaff(rs.getInt("numberStaff"));
                booth.setPriority(rs.getInt("priority"));
                booth.setSponsor(rs.getString("sponsor"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listBooth;
    }

    /**
     * Get idExhibitor from name in listExhibitor
     * @param nameExhibitor is name of a Exhibitor
     * @return id of that Exhibitor
     */
    public String getID(String nameExhibitor) {
        for (Exhibitor ex : listExhibitor) {
            if (ex.getName().equalsIgnoreCase(nameExhibitor)) {
                return ex.getId();
            }
        }
        return "";
    }

    /**
     * Create name of booth folow by name of Zone and not same with other
     * name of booth in this zone
     * @param z is Zone
     * @return a string is name of new Zone
     */
    public String createName(Zone z){
        try{
            ResultSet rs = DataUtility.excuteQuery("select count(*) as numBooth from Booth where status = 1 and idZone  = \'" + z.getIDzone() + "\'");
            String name = z.getName() + "-";
            int a = 0;
            if(rs.next()){
                a = rs.getInt("numBooth");
            }

            if(a >= 100){
                name = name + a;
            } else if(a >= 10){
                name = name + "0" + a;
            } else if(a < 10){
                name = name + "00" + a;
            }
            return name;
        } catch(Exception e){
           e.printStackTrace();
           return "";
        }
    }

    /**
     * Load list Exhibitor from database, every exhibitor must be has
     * status = 1
     * @return list Exhibitor
     */
    public Vector<Exhibitor> loadExhibitor() {
        try {
            listExhibitor = new Vector<Exhibitor>();

            ResultSet rs = DataUtility.excuteQuery("Select * from Exhibitor where status = 1");
            while (rs.next()) {
                Exhibitor exhibitor = new Exhibitor();
                listExhibitor.add(exhibitor);

                exhibitor.setId(rs.getString("id"));
                exhibitor.setName(rs.getString("Name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listExhibitor;
    }

    /**
     * Load list Zone from database, every Zone must be has
     * status = 1
     * @return list zone
     */
    public Vector<Zone> loadZone() {
        try {
            listZone = new Vector<Zone>();

            ResultSet rs = DataUtility.excuteQuery("Select * from Zone where status = 1");
            while (rs.next()) {
                Zone zone = new Zone();
                listZone.add(zone);

                zone.setIDzone(rs.getString("idZone"));
                zone.setMoney(rs.getFloat("money"));
                zone.setName(rs.getString("name"));
                zone.setNumberBooth(rs.getInt("numberBooth"));
                zone.setSize(rs.getString("Size"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listZone;
    }

    /**
     * Filter list exhibitor folow by a string, if name of a Exhibitor
     * containt that string name will be add into subList
     * @param name is String
     * @return list exhibitor containt this string
     */
    public Vector<Exhibitor> filterExhibitor(String name) {
        Vector<Exhibitor> subList = new Vector<Exhibitor>();
        name = name.toUpperCase().trim();
        
        for (Exhibitor exhibitor : listExhibitor) {
            String nameEx = exhibitor.getName();
            nameEx = nameEx.toUpperCase().trim();
            
            if (nameEx.contains(name)) {
                subList.add(exhibitor);
            }
        }
        return subList;
    }

    /**
     * Generate new ID of Booth and not same other Id has in database
     * @return String new ID
     */
    public String createID() {
        String id = "BT";
        int amount = 0;
        try {
            ResultSet rs = DataUtility.excuteQuery("select count(*) from Booth");
            if(rs.next()){
                amount = rs.getInt(1);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        String number = "" + (amount + 1);
        if (number.length() == 1) {
            id = id + "000";
        } else if (number.length() == 2) {
            id = id + "00";
        } else if (number.length() == 3) {
            id = id + "0";
        }
        id = id + number;
        return id;
    }

    /**
     * Return a booth folow by it's index in array
     * @param index is specify location of it in array
     * @return a object booth
     */
    public Booth getByIndex(int index) {
        return listBooth.get(index);
    }

    /**
     * Get information of object Booth put into HashMap.
     * Then call method executeProcedure to insert object into database
     * @param booth is new Object to get information pass into database
     */
    public void insert(Booth booth) {
        HashMap fiels = new HashMap();

        fiels.put("dateCreate", booth.getDateCreate());
        fiels.put("id", booth.getId());
        fiels.put("idExhibitor", booth.getIdExhibitor());
        fiels.put("idZone", booth.getIdZone());
        fiels.put("moneyService", booth.getMoneyService());
        fiels.put("name", booth.getName());
        fiels.put("numberStaff", booth.getNumberStaff());
        fiels.put("sponsor", booth.getSponsor());
        fiels.put("priority", booth.getPriority());


        DataUtility.executeProcedure("insertBooth", fiels);
    }

    /**
     * Get information of object Booth put into HashMap.
     * Then call method executeProcedure to update object into database
     * @param booth is new Object to get information pass into database
     */
    public void update(Booth booth) {
        HashMap fiels = new HashMap();

        fiels.put("dateCreate", booth.getDateCreate());
        fiels.put("id", booth.getId());
        fiels.put("idExhibitor", booth.getIdExhibitor());
        fiels.put("idZone", booth.getIdZone());        
        fiels.put("moneyService", booth.getMoneyService());
        fiels.put("name", booth.getName());
        fiels.put("numberStaff", booth.getNumberStaff());
        fiels.put("sponsor", booth.getSponsor());
        fiels.put("priority", booth.getPriority());


        DataUtility.executeProcedure("updateBooth", fiels);
    }

    /**
     * call method load listReport folow list zone.
     * Print report about assignment booth base on list zone and date.
     * print Exhibitor rent booth
     * @param subList
     * @param date used to check date rent must be between date and today
     */
    public void printReport(Vector<Zone> subList, String date) {
        try {
            //call method loadReport
            loadReport(subList, date);
            //Create a file
            String path = "Report.pdf";
            File file = new File(path);
            //Instantiation of document object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            //Creation of PdfWriter object
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();

            Paragraph title1 = new Paragraph("\t\tReport about booth assignments and analyzing booth\n\n", FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, new BaseColor(0, 0, 0)));
            Chapter chapter1 = new Chapter(title1, 1);

            chapter1.setNumberDepth(0);

            for (int i = 1; i <= listReport.size(); i++) {
                Report re = listReport.get(i - 1);

                Paragraph title11 = new Paragraph(re.getIdExhibitor() + " " + re.getNameExhibitor() + " belong with company " + re.getCompany(),
                        FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, new BaseColor(255, 0, 0)));

                Section section = chapter1.addSection(title11);

                Paragraph someSectionText = new Paragraph("Was rent a Booth has properties below:");
                section.add(someSectionText);

                someSectionText = new Paragraph("ID: " + re.getIdBooth());
                section.add(someSectionText);

                someSectionText = new Paragraph("Name: " + re.getNameBooth());
                section.add(someSectionText);

                someSectionText = new Paragraph("Size: " + re.getSize());
                section.add(someSectionText);

                someSectionText = new Paragraph("Fees: " + re.getMoney());
                section.add(someSectionText);

                someSectionText = new Paragraph("Money Service: " + re.getMoneyService());
                section.add(someSectionText);

                someSectionText = new Paragraph("Date rent: " + re.getDateCreate() + "\n");
                section.add(someSectionText);
                section.add(new Paragraph(""));
            }

            document.add(chapter1);
            document.close();

            //Open file
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + path);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Load list report folow by list zone and date
     * @param subList
     * @param date used to check date rent must be between date and today
     */
    public void loadReport(Vector<Zone> subList, String date) {
        try {
            String today = DateUtility.today();
            listReport = new Vector<Report>();

            for (Zone z : subList) {

                HashMap fiels = new HashMap();
                fiels.put("idZone", z.getIDzone());
                fiels.put("date", date);
                fiels.put("today", today);

                ResultSet rs = DataUtility.executeProcedure("ReportBooth", fiels, true);
                while (rs.next()) {
                    Report re = new Report();
                    listReport.add(re);

                    re.setCompany(rs.getString("Company"));
                    re.setDateCreate(rs.getString("DateCreate"));
                    re.setIdBooth(rs.getString("IdBooth"));
                    re.setIdExhibitor(rs.getString("IdExhibitor"));
                    re.setMoney(rs.getFloat("Money"));
                    re.setMoneyService(rs.getFloat("MoneyService"));
                    re.setNameBooth(rs.getString("NameBooth"));
                    re.setNameExhibitor(rs.getString("NameExhibitor"));
                    re.setNameZone(rs.getString("NameZone"));
                    re.setSize(rs.getString("Size"));
                }
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
